package com.bj.zspace.cat.controller;

import com.dianping.cat.Cat;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.ForkedTransaction;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author:liuwenqing
 * @Date:2024/11/12 20:58
 * @Description:
 **/
@RestController
public class CatTest {

    @GetMapping(value = "/transaction")
    public String testTransaction() {

        Transaction t = Cat.newTransaction("URL", "pageName");
        try {
            Cat.logEvent("URL.Server", "serverIp", Event.SUCCESS, "ip=127.0.0.1");
            Cat.logMetricForCount("metric.Key");
            Cat.logMetricForDuration("metric.Key", 5);
            int rand = new Random().nextInt(10);
            if (rand > 5) {
               int zero = 1 / 0;
            }
            t.setStatus(Transaction.SUCCESS);
        } catch (Exception e) {
            t.setStatus(e);
            Cat.logError(e);
        } finally {
            t.complete();
        }

        return "ok";
    }
    @GetMapping(value = "/event")
    public String testEvent() {
        Cat.logEvent("event", "logEvent");
        Event event = Cat.newEvent("event", "newEvent");
        event.setStatus("0");
        event.complete();
        Cat.logError(new RuntimeException("custom error"));
        return "ok";
    }
    @GetMapping("/fork_transaction")
    public String forkTransaction() throws InterruptedException {

        ForkedTransaction forkedTransaction = Cat.newForkedTransaction("fork_transaction", "test");
        TimeUnit.SECONDS.sleep(1);
        forkedTransaction.fork();
        TimeUnit.SECONDS.sleep(1);
        forkedTransaction.fork();
        TimeUnit.SECONDS.sleep(1);
        forkedTransaction.fork();
        TimeUnit.SECONDS.sleep(1);
        forkedTransaction.fork();

        forkedTransaction.complete();

        return "success";

    }

    @GetMapping("/test_fork")
    public void testForkedTransaction() throws Exception {
        Transaction t = Cat.newTransaction("ForkedRoot", "Root");
        ForkedTransaction t1 = Cat.newForkedTransaction("ForkedChild", "Child1"); //在当前消息树中埋点桥接节点
        new TimedThread(t1, 100).start();
        TimeUnit.MILLISECONDS.sleep(200);
        t.setStatus(Message.SUCCESS);
        t.complete();
    }

    static class TimedThread extends Thread {
        private ForkedTransaction m_transaction;

        private int m_timeout;

        public TimedThread(ForkedTransaction t, int timeout) {
            m_transaction = t;
            m_timeout = timeout;
        }

        @Override
        public void run() {
            m_transaction.fork();
            try {
                TimeUnit.MILLISECONDS.sleep(m_timeout);
                Cat.logEvent("TimedThread", "Timeout." + m_timeout);
                m_transaction.setStatus(Message.SUCCESS);
            } catch (Exception e) {
                Cat.logError(e);
                m_transaction.setStatus(e);
            } finally {
                m_transaction.complete();
            }
        }
    }

}
